package com.firemoss.ui.blanket.calloutClasses
{
	import com.firemoss.ui.blanket.AbstractBlanketOverlay;
	import com.firemoss.ui.blanket.util.Orientation;
	import com.firemoss.ui.blanket.util.OrientationResult;
	
	import flash.geom.Point;
	
	import mx.core.UIComponent;
	import mx.effects.Fade;
	
	public class AbstractCallout extends AbstractBlanketOverlay implements ICalloutDisplay
	{
		// PRIVATE PROPERTIES
		
		private var orientation : Orientation
		
		// PUBLIC PROPERTIES
		
		private var _calloutContents : UIComponent
		public function set calloutContents( value : UIComponent ) : void
		{
			_calloutContents = value
		}
		public function get calloutContents() : UIComponent
		{
			return _calloutContents
		}
		

		public var currentOrientation : Number
		
		public function set orientationPriority( value : Array ) : void
		{
			orientation.defaultOrientationPriority = value
			invalidatePosition()
			invalidateDisplayList()
		}
		public function get orientationPriority() : Array
		{
			return orientation.defaultOrientationPriority
		}
		
		private var _relativeX : Number = 0
		public function set relativeX( value : Number ) : void
		{
			_relativeX = value
			invalidatePosition()
			invalidateDisplayList()
		}
		public function get relativeX() : Number
		{
			return _relativeX
		}

		private var _relativeY : Number = 0
		public function set relativeY( value : Number ) : void
		{
			_relativeY = value
			invalidatePosition()
			invalidateDisplayList()
		}
		public function get relativeY() : Number
		{
			return _relativeY
		}
		
		private var position : Point
		
		private var positioned : Boolean = false
		
		public function AbstractCallout()
		{
			orientation = new Orientation()
			
			showEffect = new Fade()
			showEffect.duration = 500
			showEffect.target = this
			Fade(showEffect).alphaFrom = 0
			Fade(showEffect).alphaTo = 1
			
			hideEffect = new Fade()
			hideEffect.duration = 500
			hideEffect.target = this
			Fade(hideEffect).alphaFrom = 1
			Fade(hideEffect).alphaTo = 0
		}
		
		override protected function invalidatePosition() : void
		{
			if ( target )
			{
				var newOrientation : OrientationResult = orientation.getOrientedPosition( this, target, relativeX, relativeY )
				var newPosition : Point = newOrientation.position
				
				if (currentOrientation != newOrientation.orientation)
				{
					invalidateDisplayList()
				}
				
				currentOrientation = newOrientation.orientation
				
				if (!position || newPosition.x != position.x || newPosition.y != position.y)
				{
					position = newPosition
					
					x = position.x
					y = position.y
				}
			}
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList( unscaledWidth, unscaledHeight )
		}
		
	}
}